$(function () {
   obj.init();
});
var obj = new function () {
    'use strict';
    return {
        form:null,
        dataGrid:null,
        init:function () {
            this.dataGrid = new xhDataGrid($('#list'), 'hrm/staff_info/getHundredMail');
            this.dataGrid.parseToolbar().init().activeButton(['bt_list_emptyMail'],['bt_list_modifyPassword']);
            this.listener();
            this.funObj().bindDgEvent();
        },
        listener:function () {
            var $this = this;

            $('#bt_list_search').click(function () {
                $this.funObj().doSearch();
            });
            xhTool.bindDocKeyDown($('#bt_list_search'));

            //修改密码
            $('#bt_list_modifyPassword').click(function () {
                if (xhExtra.buttonDisabled($(this))) {
                    return false;
                }
                $this.funObj().changePassword();
            });
            //清除邮箱
            $('#bt_list_emptyMail').click(function () {
                if (xhExtra.buttonDisabled($(this))) {
                    return false;
                }
                $this.funObj().emptyMail();
            });
            
            // $('#bt_list_seeInfo').click(function () {
            //     if (xhExtra.buttonDisabled($(this))) {
            //         return false;
            //     }
            //     $this.funObj().mailInfo();
            // })
        },
        funObj:function () {
            var $this = this;
            return {
                doSearch:function () {
                    $this.dataGrid.reload({
                        key:xhExtra.getValue($('#list_search'))
                    })
                },
                changePassword:function () {
                    var uid = $this.dataGrid.getSelectValue('sHundredMail');
                    uid = $(uid).text();
                    if (!uid) {
                        window.top.xhTop.showError('错误', '为获取邮箱信息');
                        return false;
                    }
                    window.top.swal({
                        showCancelButton: true,
                        confirmButtonText: '确认',
                        cancelButtonText: '取消',
                        showLoaderOnConfirm: true,
                        allowOutsideClick: false,
                        title: '修改密码',
                        input: 'text',
                        preConfirm: function(password) {
                            return new Promise(function (resolve, reject) {
                                if(!password){
                                    reject('未输入密码！');
                                } else {
                                    resolve(password)
                                }
                            })
                        }
                    }).then(function (password) {
                        xhExtra.ajaxRequest('hrm/staff_info/modifyMailPassword',{uid:uid,password:password},{},function (res) {
                            window.top.xhTop.showSuccess(res.msg);
                        });
                    },function (dismiss) {
                        return false;
                    });
                },
                emptyMail:function () {
                    var staffIds = $this.dataGrid.getSelectValues('iStaffId');
                    xhExtra.askChangeStatus('hrm/staff_info/emptyEmail',{staffIds:staffIds},$this.dataGrid, '确定要清除邮箱吗？',function (res) {
                        window.top.xhTop.showSuccess(res.msg);
                    })
                },
                bindDgEvent:function () {
                    $this.dataGrid.onDblClickRow = function (rowIndex,rowData) {
                        var uid = rowData['sHundredMail'];
                        var staffName = rowData['sStaffName'];
                        uid = $(uid).text();
                        if (!uid) {
                            window.top.xhTop.showError('错误', '为获取邮箱信息');
                            return false;
                        }
                        xhExtra.ajaxRequest('hrm/staff_info/getMailInfo',{uid:uid},{},function (res) {

                            var str = '<table class="table table-hover">';
                            if (res.realname) {
                                str +=  '           <tr>' +
                                    '               <th >真实姓名</th>' +
                                    '               <td>'+res.realname+'</td>' +
                                    '           </tr>' ;
                            }
                            if (res.loginName) {
                                str +=  '           <tr>' +
                                    '               <th >账号</th>' +
                                    '               <td>'+res.loginName+'</td>' +
                                    '           </tr>' ;
                            }
                            if (res.password) {
                                str +=  '           <tr>' +
                                    '               <th>密码</th>' +
                                    '               <td>'+res.password+'</td>' +
                                    '           </tr>' ;
                            }
                            str+='</table>';
                            window.top.swal({
                                showConfirmButton: false,
                                allowOutsideClick: true,
                                title: staffName+'的百分邮箱',
                                html : str
                            });
                        });
                    }
                },
                mailInfo:function () {


                }
            }
        }
    }
};